package class03;

import java.util.*;

/**
 * <a href="https://leetcode.cn/problems/implement-stack-using-queues/">225. 用队列实现栈</a>
 *
 * 面试小细节：如果面试官问，如何用队列实现图的深度优先遍历，可以用两个队列模拟栈来实现
 */
public class Code07_TwoQueueImplementStack {

    public static class MyStack {
        public Queue<Integer> queue, help;

        public MyStack() {
            queue = new ArrayDeque<>();
            help = new ArrayDeque<>();
        }

        public void push(int x) {
            queue.offer(x);
        }

        public int pop() {
            while (queue.size() > 1) {
                help.offer(queue.remove());
            }
            int result = queue.remove();
            Queue<Integer> temp = queue;
            queue = help;
            help = temp;
            return result;
        }

        public int top() {
            while (queue.size() > 1) {
                help.offer(queue.remove());
            }
            int result = queue.remove();
            help.offer(result);
            Queue<Integer> temp = queue;
            queue = help;
            help = temp;
            return result;
        }

        public boolean empty() {
            return queue.isEmpty() && help.isEmpty();
        }
    }
}
